//
//  WJMianLayout.m
//  WJBSApp
//
//  Created by 王景伟 on 2018/11/22.
//  Copyright © 2018 王景伟. All rights reserved.
//

#import "WJMianLayout.h"

@interface WJMianLayout ()
@end

@implementation WJMianLayout

- (instancetype)initWithModel:(WJMainModel *)model {
    if (!model || !model.user) return nil;
    self = [super init];
    _model = model;
    [self getStyle];
    [self layout];
    return self;
}

- (void)getStyle {
    if ([self.model.type isEqualToString:@"image"]) {
        _style = WJMianLayoutImage;
    } else if ([self.model.type isEqualToString:@"gif"]) {
        _style = WJMianLayoutGif;
    } else if ([self.model.type isEqualToString:@"video"]) {
        _style = WJMianLayoutVideo;
    } else if ([self.model.type isEqualToString:@"audio"]) {
        _style = WJMianLayoutAudio;
    } else if ([self.model.type isEqualToString:@"html"]) {
        _style = WJMianLayoutHtml;
    } else {
        _style = WJMianLayoutText;
    }
}

- (void)layout {
    
    _isScale = NO;
    CGFloat top = kBSCellMargin;

    // 48 * 36  24 * 18
    if (_model.user.is_vip) {
        CGFloat nameW = [_model.user.name boundingRectWithSize:CGSizeMake(kBSContentW, kBSNameFont) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kBSNameFont]} context:nil].size.width;
        _vipRect = CGRectMake(nameW + kBSContentMargin, 0, kBSVipW, kBSVipH);
    }
    
    top += kBSHeaderW + kBSContentMargin;
    CGFloat titleH = [_model.text boundingRectWithSize:CGSizeMake(kBSContentW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kBSTitleFont]} context:nil].size.height;
    _titleRect = CGRectMake(kBSCellMargin, top, kBSContentW, titleH);
    
    top += titleH + kBSContentMargin;
    CGFloat imageH = 0;
    switch (_style) {
        case WJMianLayoutImage:
            if (_model.image.thumbnail_small.count > 0) {
                _imageName = _model.image.thumbnail_small[0];
                imageH = kBSContentW;
            }
            break;
        case WJMianLayoutGif:
            if (_model.gif.images.count > 0) {
                _imageName = _model.gif.gif_thumbnail[0];
            }
            break;
        case WJMianLayoutVideo:
            if (_model.video.thumbnail.count > 0) {
                _imageName = _model.video.thumbnail_small[0];
                imageH = kBSContentW;
            }
            break;
        case WJMianLayoutAudio:
            if (_model.audio.thumbnail.count > 0) {
                _imageName = _model.audio.thumbnail_small[0];
                imageH = kBSContentW;
            }
            break;
        case WJMianLayoutHtml:
            if (_model.html.thumbnail.count > 0) {
                _imageName = _model.html.thumbnail[0];
                imageH = kBSHtmlH;
            }
            break;
        default:
            imageH = 0;
            break;
    }
    
    if (_style == WJMianLayoutImage) {
        CGFloat largeImage = (CGFloat)_model.image.height * kBSContentW / _model.image.width;
        if (largeImage > kBSContentW) _isScale = YES;
    }
    _imageRect = CGRectMake(kBSCellMargin, top, kBSContentW, imageH);

    if (imageH) top += imageH + kBSContentMargin;

    if (_style == WJMianLayoutVideo || _style == WJMianLayoutAudio) {
        if (_style == WJMianLayoutVideo) {
            NSInteger min = _model.video.duration / 60;
            NSInteger sec = _model.video.duration % 60;
            _durationText = [NSString stringWithFormat:@"%02zd:%02zd",min,sec];
            _countText = [NSString stringWithFormat:@"%tu次播放",_model.video.playcount];
        }
        if (_style == WJMianLayoutAudio) {
            NSInteger min = _model.audio.duration / 60;
            NSInteger sec = _model.audio.duration % 60;
            _durationText = [NSString stringWithFormat:@"%02zd:%02zd",min,sec];
            _countText = [NSString stringWithFormat:@"%tu次播放",_model.audio.playcount];
        }
        
        NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:kBSCountFont]};
        CGFloat durationW = [_durationText boundingRectWithSize:CGSizeMake(1000, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size.width;
        _durationRect = CGRectMake(0, imageH - 20, durationW, 20);
                
        CGFloat countW = [_countText boundingRectWithSize:CGSizeMake(1000, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size.width;
        _countRect = CGRectMake(kBSContentW - countW, imageH - 20, countW, 20);
    }
    
    _commentY = top;
    _commentHeights = [NSMutableArray array];
    ///> 计算评论的高度
    for (TopComment *comment in _model.topComments) {
        comment.showText = [NSString stringWithFormat:@"%@:%@",comment.user.name,comment.content];
        CGFloat height = [comment.showText boundingRectWithSize:CGSizeMake(kBSContentW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kBSCommentFont]} context:nil].size.height;
        [_commentHeights addObject:@(height)];
        top += height + kBSContentMargin;
    }

    _tagRect = CGRectMake(kBSCellMargin, top, kBSContentW, kBSTagH);
    top += kBSTagH + kBSContentMargin;
    
    CGFloat butW = (kBSContentW - kBSCellMargin * 3) / 4.0f;
    _upButRect = CGRectMake(kBSCellMargin, top, butW, kBSButH);
    _downButRect = CGRectMake(kBSCellMargin * 2 + butW, top, butW, kBSButH);
    _forwardButRect = CGRectMake(kBSCellMargin * 3 + butW * 2, top, butW, kBSButH);
    _commentButRect = CGRectMake(kBSCellMargin * 4 + butW * 3, top, butW, kBSButH);
    
    _cellHeight = top + kBSButH;
    _model.cellHeight = _cellHeight;
}

@end
